import os
from building import *

cwd = GetCurrentDir()

Import('asenv')
ASROOT = asenv['ASROOT']

SelectCompilerCWCC()
asenv.Append(ASFLAGS=['-proc=56X'])
asenv.Append(CCFLAGS=['-proc=56X'])
asenv.Append(LINKFLAGS=['-proc=56X'])
asenv.Append(LINKFLAGS=['-srec','mpc56xx.s19','-sreceol','unix'])
asenv.Append(LINKFLAGS=['-m','_start'])
asenv.Append(LINKFLAGS=['-romaddr','0x0','-rambuffer','0x0','-gdwarf-2',
                        '-code_merging','all'])
asenv.Append(LINKFLAGS=['-map','build/any/mpc56xx.map'])

# fork env for NXP AUTOSAR BSW
env = ForkEnv(asenv)
for flgs in ['-include','asmconfig.h']:
	env['CCFLAGS'].remove(flgs)

MODULES = ['ECUM','SCHM','MCU',
           'DET','DEM',
           'CLIB_STDIO_PRINTF',
           'ASKAR','SHELL','CLIB_STRTOK_R',
           'TRACE',
           'PORT','DIO','ADC',
           'CAN','CANIF','PDUR','COM','COMM','CANTP',
           'DCM','CANNM','CANSM','NM','OSEKNM','XCP',
           'FLS','FEE','MEMIF','NVM','CLIB_MBOX',
           'MPC5634M_MLQB80','COMMONXML'
           ]

ARCH='none'
arch='mpc56xx'

if(asenv['MODULES'] is None):
    asenv['MODULES'] = MODULES
else:
    MODULES = asenv['MODULES']

asenv['ARCH'] = ARCH
asenv['arch'] = arch

if(os.getenv('NXPMCAL')):
    nxpmcal = os.getenv('NXPMCAL')
else:
    nxpmcal = 'C:/Freescale/AUTOSAR/MPC563XM_MCAL3.0_RTM_2.0.0_signed'

if(os.getenv('NXPOS')):
    nxpos = os.getenv('NXPOS')
else:
    nxpos = 'C:/Freescale/AUTOSAR/os_freescale_mpc56xxAM'

MKSymlink(nxpmcal, '%s/mcal'%(cwd))
MKSymlink(nxpos, '%s/os'%(cwd))

objs = []

env.Append(CPPPATH=['%s/config/include'%(cwd)])

for m in MODULES:
    env.Append(CPPDEFINES=['USE_%s'%(m)])

pkg = Package('https://gitlab.com/as-boards/mpc56xx-nxp.git')
MKSymlink(pkg+'/MPC5634M_MLQB80',cwd+'/MPC5634M_MLQB80')
MKSymlink(pkg+'/app',cwd+'/app')
MKSymlink(pkg+'/config',cwd+'/config')

# should be JAVA 32
#cmd = 'set PATH="c:/Program Files (x86)/Java/jre1.8.0_171/bin";%PATH% && '
#cmd += '%s/os/ssc/bin/generator/sg.exe %s/config/epc/project.epc'%(cwd,cwd)
#RunCommand(cmd)
if('ANYOS' in MODULES):
    env.Append(CPPPATH=['%s/config/epc'%(cwd),'%s/os/ssc/inc'%(cwd)])
    objs += Glob('os/ssc/src/*.c')
    objs += Glob('os/ssc/hwspec/vector.c')
    objs += Glob('config/epc/*.c')

env.Append(CPPPATH=['%s/mcal/Base_TS_T2D14M20I0R0/include'%(cwd)])
env.Append(CPPPATH=['%s/mcal/MPC563XM_MCAL3.0_RTM_2.0.0_Integration_Application/cfg/include'%(cwd)])
env.Append(CPPPATH=['%s/mcal/MPC563XM_MCAL3.0_RTM_2.0.0_Integration_Application/include'%(cwd)])
env.Append(CPPPATH=['%s/mcal/Dem_TS_T2D14M20I0R0/include'%(cwd)])
env.Append(CPPPATH=['%s/mcal/Det_TS_T2D14M20I0R0/include'%(cwd)])
env.Append(CPPPATH=['%s/mcal/MemIf_TS_T2D14M20I0R0/include'%(cwd)])
env.Append(CPPPATH=['%s/os/ssc/inc'%(cwd)])

objs += Glob('app/*.c')

if('CANIF' in MODULES):
    env.Append(CPPPATH=['%s/mcal/CanIf_TS_T2D14M20I0R0/include'%(cwd)])

if('CAN' in MODULES):
    env.Append(CPPPATH=['%s/mcal/Can_TS_T2D14M20I0R0/include'%(cwd)])
    objs += Glob('config/src/Can*.c')
    objs += Glob('mcal/Can_TS_T2D14M20I0R0/src/*.c')
    objs += Glob('mcal/SchM_TS_T2D14M20I0R0/src/SchM_Can.c')
    env.Append(CPPDEFINES=['Can_ConfigSet_PC=Can_ConfigData',
                           'ControllerConfigs_PC=Can_ControllerCfgData'])
    asenv.Append(CPPDEFINES=['USE_ANYCAN','USE_CAN_HOH_ID'])

if('DIO' in MODULES):
    env.Append(CPPPATH=['%s/mcal/Dio_TS_T2D14M20I0R0/include'%(cwd)])
    objs += Glob('config/src/Dio*.c')
    objs += Glob('mcal/Dio_TS_T2D14M20I0R0/src/*.c')
    objs += Glob('mcal/SchM_TS_T2D14M20I0R0/src/SchM_Dio.c')

if('MCU' in MODULES):
    env.Append(CPPPATH=['%s/mcal/Mcu_TS_T2D14M20I0R0/include'%(cwd)])
    objs += Glob('config/src/Mcu*.c')
    objs += Glob('mcal/Mcu_TS_T2D14M20I0R0/src/*.c')
    env.Append(CPPDEFINES=['MCU_INIT_CONFIG_PC=McuConfigData'])
    objs += Glob('mcal/SchM_TS_T2D14M20I0R0/src/SchM_Mcu.c')

if('SCHM' in MODULES):
    env.Append(CPPPATH=['%s/mcal/SchM_TS_T2D14M20I0R0/include'%(cwd)])
    objs += Glob('config/src/SchM*.c')

if('PORT' in MODULES):
    env.Append(CPPPATH=['%s/mcal/Port_TS_T2D14M20I0R0/include'%(cwd)])
    objs += Glob('config/src/Port*.c')
    objs += Glob('mcal/Port_TS_T2D14M20I0R0/src/*.c')
    env.Append(CPPDEFINES=['Port_ConfigPC=PortConfigData'])
    objs += Glob('mcal/SchM_TS_T2D14M20I0R0/src/SchM_Port.c')

if('ADC' in MODULES):
    env.Append(CPPPATH=['%s/mcal/Adc_TS_T2D14M20I0R0/include'%(cwd)])
    objs += Glob('config/src/Adc*.c')
    objs += Glob('mcal/Adc_TS_T2D14M20I0R0/src/*.c')
    env.Append(CPPDEFINES=['AdcConfigSet_0=AdcConfig'])
    objs += Glob('mcal/SchM_TS_T2D14M20I0R0/src/SchM_Adc.c')

if('FLS' in MODULES):
    env.Append(CPPPATH=['%s/mcal/Fls_TS_T2D14M20I0R0/include'%(cwd)])
    objs += Glob('config/src/Fls*.c')
    objs += Glob('mcal/Fls_TS_T2D14M20I0R0/src/*.c')
    env.Append(CPPDEFINES=['FlsConfigSet_0=FlsConfigSet'])
    objs += Glob('mcal/SchM_TS_T2D14M20I0R0/src/SchM_Fls.c')
    asenv.Append(CPPDEFINES=['FEE_BANK1_OFFSET=0x00000',
                             'FEE_BANK2_OFFSET=0x10000',
                             'FEE_BANK1_LENGTH=0x10000',
                             'FEE_BANK2_LENGTH=0x10000'])

objs = env.Object(objs)

if('FLASH' in MODULES):
    objs += Glob('../../../as.infrastructure/arch/mpc56xx/mcal/Flash.c')

if('MPC5634M_MLQB80' in MODULES):
    srcs = Glob('MPC5634M_MLQB80/Project/src/*.c')
    SrcRemove(srcs, ['MPC55xx_init_debug.c'])
    objs += srcs
    asenv.Append(LINKFLAGS=['-lcf','%s/MPC5634M_MLQB80/Project/lcf/MPC5634M_MLQB80.lcf'%(cwd)])
    asenv.Append(CPPPATH=['%s/MPC5634M_MLQB80/Project/src'%(cwd)])
    MKSymlink('%s/MPC5634M_MLQB80/Project/src/MPC5634M_MLQB80.h'%(cwd),
              '%s/MPC5634M_MLQB80/Project/src/mpc56xx.h'%(cwd))

asenv.Append(CPPPATH=['%s/com/as.application/board.posix/common'%(ASROOT)])
if('ANYOS' in MODULES):
    asenv.Append(CPPDEFINES=['USE_OS_CONST_ID','_TASK_t=uint16',
                             '_COUNTER_t=uint16','_ALARM_t=uint16',
                             '_RESOURCE_t=uint16','OSDEFAULTAPPMODE=0'])

if('SHELL' in MODULES):
    asenv.Append(CPPDEFINES=['ENABLE_SHELL_ECHO_BACK'])

Return('objs')
